package com.wx.wxpush.util;

import com.alibaba.fastjson.JSONObject;
import com.wx.wxpush.entiy.TempData;
import com.wx.wxpush.entiy.TemplateData;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

/**
 *@ClassName Pusher
 *@Description TODO
 *@Author chen
 *@Date 2022/8/22 09:54
 */
@Slf4j
public class Pusher {
    /**
     * 测试号的appId和secret
     */
   // private static String appId = "xxx";
   // private static String secret = "xxx";
    //模版id
    //private static String morningTemplateId = "mKzKeP-GzYjNrkDrz5S63NDKYXQcAOGZARgy_VGIHWM";
    private static String morningTemplateId = "ubs71G6iBYckUPTGOVWsBYfIMr3kLlgLvpuLAFa2D6A";
//
//    private static String nightTemplateId = "xx5wJq6MJhj82dPgIdd3YA_0InZx_sux4WwgUgr5Tfk";

    //线上 版本服务号
    public static int  pushMorning(
        TempData templateData){
    //1，配置
    WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
    wxStorage.setAppId(templateData.getAppid());
    wxStorage.setSecret(templateData.getSecret());
    WxMpService wxMpService = new WxMpServiceImpl();
    wxMpService.setWxMpConfigStorage(wxStorage);
    //2,推送消息
    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            .toUser(templateData.getOpenid())
            .templateId(templateData.getMorningTemplateId())
            //.url("https://30paotui.com/")//点击模版消息要访问的网址
            .build();
    //3,如果是正式版发送模版消息，这里需要配置你的信息
    //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
    //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
    //填写变量信息，比如天气之类的
        String beizhu =""+CaiHongPi.getJinJu();
        if(Integer.parseInt(JiNianRi.getJieHun(templateData.getBirthday())) % 365 == 0){
            beizhu = "今天是你的生日！";
        }
        if(Integer.parseInt(JiNianRi.getLianAi(templateData.getLianaitime())) % 365 == 0){
            beizhu = "今天是恋爱纪念日！";
        }
    System.out.println(templateData.getBeizhu()+"templateData.getBeizhu()");
    JSONObject todayWeather = Tianqi.getTianqiDist(templateData.getDistrict());
    JSONObject location = Tianqi.getLocation(templateData.getDistrict());
    String bei=templateData.getBeizhu();
    templateMessage.addData(new WxMpTemplateData("first",  "欢迎 各位 ~ 今天是"+ todayWeather.getString("week")+" / 天气："+todayWeather.getString("text_day"),"#00BFFF"));
    templateMessage.addData(new WxMpTemplateData("keyword1","你在"+location.getString("city")+" "+"最低温 "+todayWeather.getString("low") +" 度 / 最高温"+todayWeather.getString("high")+ " 度 ","#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword2","距离你生日还有 "+JiNianRi.getBirthDay(templateData.getBirthday())+" 天，"+"我们一起走过了 "+
                JiNianRi.getLianAi(templateData.getLianaitime())+" 天","#FF69B4"));

        templateMessage.addData(new WxMpTemplateData("keyword3",  todayWeather.getString("date"),"#FF69B4"));
//
//        templateMessage.addData(new WxMpTemplateData("keyword",CaiHongPi.getCaiHongPi(),"#FF69B4"));
//    templateMessage.addData(new WxMpTemplateData(templateData.getLianai(),JiNianRi.getLianAi(templateData.getLianaitime())+"","#FF1493"));
//    templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getBirthDay(templateData.getBirthday())+"","#FFA500"));
//    templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
//    //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
////        templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
//    //templateMessage.addData(new WxMpTemplateData("beizhu",bei+" ","#FF6347"));
//    templateMessage.addData(new
//            WxMpTemplateData("beizhu",location.getString("city")+"","#FF6347"));
    templateMessage.addData(new
            WxMpTemplateData("remark",templateData.getRemark()+" / 等等还有重要事情 "+beizhu,"#FF007F"));


//        if(JiNianRi.getLinZhen() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
    //templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));


    try {
        System.out.println(templateMessage.toJson());
        System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
    } catch (Exception e) {
        System.out.println("推送失败：" + e.getMessage());
        e.printStackTrace();
    }
    return 1;
}
//    public static int  pushMorning(
//            TempData templateData){
//        //1，配置
//        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
//        wxStorage.setAppId(templateData.getAppid());
//        wxStorage.setSecret(templateData.getSecret());
//        WxMpService wxMpService = new WxMpServiceImpl();
//        wxMpService.setWxMpConfigStorage(wxStorage);
//        //2,推送消息
//        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
//                .toUser(templateData.getOpenid())
//                .templateId(templateData.getMorningTemplateId())
//                //.url("https://30paotui.com/")//点击模版消息要访问的网址
//                .build();
//        //3,如果是正式版发送模版消息，这里需要配置你的信息
//        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
//        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
//        //填写变量信息，比如天气之类的
//        System.out.println(templateData.getBeizhu()+"templateData.getBeizhu()");
//        JSONObject todayWeather = Tianqi.getTianqiDist(templateData.getDistrict());
//        JSONObject location = Tianqi.getLocation(templateData.getDistrict());
//        String bei=templateData.getBeizhu();
//        templateMessage.addData(new WxMpTemplateData(templateData.getRiqi(),todayWeather.getString("date") + "  "+ todayWeather.getString("week"),"#00BFFF"));
//        templateMessage.addData(new WxMpTemplateData("tianqi",todayWeather.getString("text_day"),"#FF4500"));
//        templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));
//        templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));
//        templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));
//        templateMessage.addData(new WxMpTemplateData(templateData.getLianai(),JiNianRi.getLianAi(templateData.getLianaitime())+"","#FF1493"));
//        templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getBirthDay(templateData.getBirthday())+"","#FFA500"));
//        templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
//        //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
////        templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
//        //templateMessage.addData(new WxMpTemplateData("beizhu",bei+" ","#FF6347"));
//        templateMessage.addData(new
//                WxMpTemplateData("beizhu",location.getString("city")+"","#FF6347"));
//        templateMessage.addData(new
//                WxMpTemplateData("remark",templateData.getRemark()+"","#FF007F"));
//
//        String beizhu ="";
//        if(Integer.parseInt(JiNianRi.getJieHun(templateData.getLianaitime())) % 365 == 0){
//            beizhu = templateData.getBeizhu();
//        }
//        if(Integer.parseInt(JiNianRi.getLianAi(templateData.getLianaitime())) % 365 == 0){
//            beizhu = "今天是恋爱纪念日！";
//        }
////        if(JiNianRi.getLinZhen() % 365 == 0){
////            beizhu = "今天是结婚纪念日！";
////        }
//        //templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));
//
//
//        try {
//            System.out.println(templateMessage.toJson());
//            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
//        } catch (Exception e) {
//            System.out.println("推送失败：" + e.getMessage());
//            e.printStackTrace();
//        }
//        return 1;
//    }



    public static void pushNight(String openId,String appId,String secret,String nightTemplateId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(nightTemplateId)
                //.url("https://30paotui.com/")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        //填写变量信息，比如天气之类的
        JSONObject todayWeather = Tianqi.getTomorrowTianqi();

        templateMessage.addData(new WxMpTemplateData("riqi",todayWeather.getString("date") + "  "+ todayWeather.getString("week"),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",todayWeather.getString("text_day"),"#FF4500"));
        templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));
       // templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#FF1493"));
        //templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
        //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
//        templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
        String beizhu = "xxx";
//        if(JiNianRi.getJieHun() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
//        if(Integer.parseInt(JiNianRi.getLianAi()) % 365 == 0){
//            beizhu = "今天是恋爱纪念日！";
//        }
//        if(JiNianRi.getLinZhen() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));


        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
    private static String appId = "wx79cf821dfab73a99";
    private static String secret = "a95cd88b40ed075902a4bb48f7b58153";
    //模版id
   // private static String morningTemplateId = "ubs71G6iBYckUPTGOVWsBYfIMr3kLlgLvpuLAFa2D6A";

    private static String nightTemplateId = "xx5wJq6MJhj82dPgIdd3YA_0InZx_sux4WwgUgr5Tfk";
    public static int  pushMorning1(String openId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(morningTemplateId)
                //.url("https://30paotui.com/")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        //填写变量信息，比如天气之类的
        JSONObject todayWeather = Tianqi.getTianqiDist("350203");
        //JSONObject location = Tianqi.getLocation(tt.getDistrict());
        templateMessage.addData(new WxMpTemplateData("first","恋爱告急" + "  "+ todayWeather.getString("week"),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("keyword2",todayWeather.getString("date") + "  "+ todayWeather.getString("week"),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("keyword1",todayWeather.getString("text_day")+ "  ","#FF4500"));
        templateMessage.addData(new WxMpTemplateData("remark","这是我们的恋爱告急"+ "  ","#FF4500"));


        //templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));
       // templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));
//        //templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));
//        templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#FF1493"));
//        templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#FFA500"));
//        //templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
        //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
//        templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
//        templateMessage.addData(new
//                WxMpTemplateData("beizhu",location.getString("city")+"","#FF6347"));

        String beizhu ="";
//        if(Integer.parseInt(JiNianRi.getJieHun(tt.getLianaitime())) % 365 == 0){
//            beizhu = tt.getBeizhu();
//        }
//        if(Integer.parseInt(JiNianRi.getLianAi(tt.getLianaitime())) % 365 == 0){
//            beizhu = "今天是恋爱纪念日！";
//        }
//        if(JiNianRi.getJieHun() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
//        if(Integer.parseInt(JiNianRi.getLianAi()) % 365 == 0){
//            beizhu = "今天是恋爱纪念日！";
//        }
//        if(JiNianRi.getLinZhen() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
        //templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));


        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
        return 1;
    }
}
